<template>
	<view class="container account-container full-container">
		<up-sticky bgColor="#fff">
			<up-tabs :list="accTypeList" lineWidth="30" :itemStyle="{width:'50%',height: '44px'}"
				@change="changeTab"></up-tabs>
		</up-sticky>

		<!-- uni-swipe-action 是最外层包裹性质的容器 -->
		<uni-swipe-action class="list-container">
			<geek-list ref="list" :api="fetchList" isPage>
				<block #default="{item}">
					<uni-swipe-action-item :right-options="options" @click="handleDelete($event,item)">
						<uni-card shadow="none" :title="item.accountName" :extra="item.accountKey">
							<view class="bottom flex justify-between">
								<view class="tag">
									账号类型：
									<uni-tag text="支付宝" type="primary" size="mini" v-if="item.accountType=='alipay'" />
									<uni-tag text="银行卡" type="warning" size="mini" v-if="item.accountType=='bank'" />
									<uni-tag text="微信" type="success" size="mini" v-if="item.accountType=='wechat'" />
								</view>
								<span>
									支持方式：
									<uni-tag text="扫码" type="error" size="mini" v-if="item.paymentWay=='qr'" />
									<uni-tag text="转账" type="error" size="mini" v-if="item.paymentWay=='trans'" />
								</span>
								<!-- <u-icon name="edit-pen" :size="30" color="#999999"></u-icon> -->

								<u-switch v-model="item.isDefault" activeColor="#5ac725"
									inactiveColor="rgb(230, 230, 230)" @change="handleSwitch(item)"
									style="transform:scale(0.7)"></u-switch>
							</view>
						</uni-card>
					</uni-swipe-action-item>
				</block>
			</geek-list>
		</uni-swipe-action>

		<view class="btn-group">
			<up-button icon="plus" @tap="toAddAccount"
				color="linear-gradient(to right, rgb(0, 85, 255), rgb(0, 170, 255))" hairline shape="circle"
				text="添加收银账号"></up-button>
		</view>
	</view>
</template>

<script>
	import {
		list,
		remove,
		changeStatus
	} from "@/api/seller/cashier"

	export default {
		data() {
			return {
				currType: 'alipay',
				accTypeList: [{
					name: '支付宝',
					value: 'alipay'
				}, {
					name: '微信',
					value: 'wechat'
				}, {
					name: '银行卡',
					value: 'bank'
				}],
				// 这个是滑动后的配置项
				options: [{
					text: '编辑', // 显示的文本内容
					style: {
						backgroundColor: '#2979ff' // 按钮的背景颜色
					}
				}, {
					text: '删除', // 显示的文本内容
					style: {
						backgroundColor: '#ff0000' // 按钮的背景颜色
					}
				}]
			};
		},
		onShow() {
			if (this.$refs?.list) {
				this.$refs.list.resetList();
			}
		},
		methods: {
			fetchList(data = {}) {
				data.pageSize = 10
				data.accountType = this.currType
				return list(data)
			},

			toAddAccount() {
				uni.navigateTo({
					url: '/pages/account/add'
				});
			},

			changeTab(obj) {
				this.currType = obj?.value
				this.$refs.list.resetList()
			},

			handleDelete({
				index
			}, res) {
				if (index === 1) {
					remove(res.id).then(response => {
						uni.showToast({
							title: '删除成功',
							mask: false,
							icon: "none",
							duration: 3000
						});

						this.$refs.list.resetList()
					})
				} else {
					uni.navigateTo({
						url:`/pages/account/add?id=${res.id}`
					})
				}
			},

			handleSwitch(res) {
				console.log(JSON.stringify(res))
				let title = "";
				if (res.isDefault) {
					title = "账号开启"
					res.status = 0;
				} else {
					title = "账号关闭"
					res.status = 1;
				}
				changeStatus(res).then(response => {
					uni.showToast({
						title: title + '成功',
						mask: false,
						icon: "none",
						duration: 3000
					});
					this.$refs.list.resetList();
				})
			},
		}
	};
</script>

<style lang="scss" scoped>
	.account-container {
		display: flex;
		flex-direction: column;

		.list-container {
			flex: 1;
			overflow: hidden;
			display: flex;
			flex-direction: column;
		}

		&::v-deep {

			.li-box,
			.uni-card {
				margin: 0 !important;
			}

			.uni-card {
				border: none;
				border-bottom: 1px solid #f2f2f2;
				border-top: 1px solid #f2f2f2;
				border-radius: 0;
			}

			.uni-swipe {
				padding-top: 24rpx !important;
			}


			.btn-group {
				padding: 30rpx 40rpx;

				.uicon-plus {
					color: #fff !important;
					margin-right: 10rpx !important;
				}
			}
		}
	}

	.container {
		flex-direction: column;
		height: 100rpx;
	}

	.content {
		background-color: #ffffff;
		margin: 15rpx auto;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			font-weight: bold;
			font-size: 34rpx;

			.phone {
				margin-left: 60rpx;
			}

			.tag {
				display: flex;
				font-weight: normal;
				align-items: center;

				text {
					display: block;
					width: 68rpx;
					height: 34rpx;
					line-height: 34rpx;
					color: #ffffff;
					font-size: 20rpx;
					border-radius: 6rpx;
					text-align: center;
					margin-left: 5rpx;
					background-color: rgb(49, 145, 253);
				}
			}
		}

		.bottom {
			display: flex;
			margin-top: 30rpx;
			font-size: 28rpx;
			justify-content: space-between;
			color: #999999;
		}
	}

	.addSite {
		display: flex;
		justify-content: space-around;
		width: 600rpx;
		line-height: 100rpx;
		position: fixed;
		bottom: 130rpx;
		left: 80rpx;
		background-color: red;
		border-radius: 60rpx;
		font-size: 30rpx;

		.add {
			display: flex;
			align-items: center;
			color: #ffffff;

			.icon {
				margin-right: 10rpx;
			}
		}
	}
</style>